package com.wang.cloud.manage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wang.cloud.common.core.api.ApiResult;
import com.wang.cloud.manage.domain.co.MenuAddCo;
import com.wang.cloud.manage.domain.co.MenuPageCo;
import com.wang.cloud.manage.domain.co.MenuUpdateCo;
import com.wang.cloud.manage.domain.co.MenuUpdateStatusCo;
import com.wang.cloud.manage.domain.vo.MenuPageVo;
import com.wang.cloud.manage.domain.vo.MenuTreeVo;
import com.wang.cloud.manage.domain.vo.MenuVo;
import com.wang.cloud.manage.service.IMenuService;
import com.wang.cloud.manage.service.manage.MenuManageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统-菜单权限表 前端控制器
 * </p>
 *
 * @author wy
 * @since 2024-07-15
 */
@Tag(name = "系统-菜单权限")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/manage/menu")
public class MenuController {

    private final IMenuService service;

    private final MenuManageService manageService;

    @Operation(summary = "分页查询")
    @GetMapping("page")
    public ApiResult<IPage<MenuPageVo>> page(MenuPageCo pageCo) {
        return ApiResult.success(service.page(pageCo));
    }

    @Operation(summary = "获取详情")
    @GetMapping("info")
    public ApiResult<MenuVo> page(@RequestParam Long id) {
        return ApiResult.success(service.getInfo(id));
    }

    @Operation(summary = "查询路由树")
    @GetMapping("base/tree")
    public ApiResult<List<MenuTreeVo>> getTree(@Parameter(description = "菜单状态（0正常 1停用）")
                                               @RequestParam(required = false) String status) {
        return ApiResult.success(service.getTree(status));
    }

    @Operation(summary = "根据上级路由id，查询下级路由列表")
    @GetMapping("sub/list")
    public ApiResult<List<MenuVo>> getSubBaseListByParentId(
            @Parameter(description = "上级菜单id") @RequestParam(required = false, defaultValue = "0") Long parentId) {
        return ApiResult.success(service.getSubBaseListByParentId(parentId));
    }

    @Operation(summary = "新增")
    @PostMapping("add")
    public ApiResult<Long> add(@Validated @RequestBody MenuAddCo addCo) {
        Long id = manageService.addMenu(addCo);
        return ApiResult.success(id);
    }

    @Operation(summary = "更新")
    @PostMapping("update")
    public ApiResult update(@Validated @RequestBody MenuUpdateCo updateCo) {
        manageService.updateMenu(updateCo);
        return ApiResult.success();
    }


    @Operation(summary = "更新-路由状态")
    @PostMapping("status/update")
    public ApiResult updateMenuStatus(@Validated @RequestBody MenuUpdateStatusCo co) {
        manageService.updateMenuStatus(co);
        return ApiResult.success();
    }

    @Operation(summary = "删除")
    @DeleteMapping("del")
    public ApiResult del(@RequestParam Long id) {
        manageService.delMenu(id);
        return ApiResult.success();
    }
}
